/**
 * 
 */
package com.xpalive.web.common;

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.xpalive.finals.Finals;

/**
 * @author xiongping
 * @IDE eclipse 4.3
 * @time 2015年5月21日 下午1:45:05
 */
public class BaseService<T extends BaseDomain> {
	@Autowired
	protected BaseMapper<T> baseMapper;

	private Map<String, Object> map = new HashMap<String, Object>();

	protected Map<String, Object> map(T t) {
		mapSetVal(baseMapper.select(t));
		return map;
	}

	protected Map<String, Object> save(T t) {
		t.setCreateTime(Calendar.getInstance().getTimeInMillis());
		baseMapper.insert(t);
		map.put(Finals.JSON_STATUS, Finals.JSON_STATUS_SUCCESS);
		map.put(Finals.JSON_MSG, Finals.JSON_MSG_SUCCESS);
		map.put(Finals.JSON_DATA, t);
		return map;
	}

	protected Map<String, Object> del(T t) {
		baseMapper.del(t);
		map.put(Finals.JSON_STATUS, Finals.JSON_STATUS_SUCCESS);
		map.put(Finals.JSON_MSG, Finals.JSON_MSG_SUCCESS);
		map.put(Finals.JSON_DATA, t);
		return map;
	}

	private void mapSetVal(List<T> list) {
		if (list != null && !list.isEmpty()) {
			map.put(Finals.JSON_STATUS, Finals.JSON_STATUS_SUCCESS);
			map.put(Finals.JSON_MSG, Finals.JSON_MSG_SUCCESS);
			map.put(Finals.JSON_DATA, list);
		} else {
			map.put(Finals.JSON_STATUS, Finals.JSON_STATUS_NODATE);
			map.put(Finals.JSON_MSG, Finals.JSON_MSG_NODATE);
			map.put(Finals.JSON_DATA, list);
		}
	}
}
